<?php

function e($var)
{
	return htmlspecialchars($var, ENT_QUOTES, 'UTF-8');
}

function fill_table($rows, $perPage)
{
	$rowCount = $perPage - count($rows);

	for ($i = 0; $i < $rowCount; ++$i)
	{
		echo '<tr class="empty"><td colspan="999">&nbsp;</td></tr>';
	}
}

function form_errors(AgaviValidationManager $vm, $field)
{
	if ($vm->hasError($field) === false)
	{
		return '';
	}

	$result = '<ul class="errors">';

	foreach ($vm->getErrorMessages($field) as $v)
	{
		$result .= '<li>' . $v . '</li>';
	}

	$result .= '</ul>';

	return $result;
}

function data_pager(Doctrine_Pager $pager, $route, $param = 'page', array $params = array())
{
	$ro = AgaviContext::getInstance('web')->getRouting();

	$currentPage = $pager->getPage();
	$pageCount   = $pager->getLastPage();

	if ($pageCount <= 1)
	{
		return '';
	}

	$result = 'Strony (' . $pageCount . '): ';

	$result .= $currentPage > 2
		? '<a class="page-first" href="' . $ro->gen($route, array_merge(array($param => 1), $params)) . '">&laquo; Pierwsza</a> '
		: '<span class="page-first">&laquo; Pierwsza</span> ';
		#: '';

	$result .= $currentPage > 1
		? '<a class="page-prev" href="' . $ro->gen($route, array_merge(array($param => $currentPage - 1), $params)) . '">&lsaquo; Poprzednia</a> '
		: '<span class="page-prev">&lsaquo; Poprzednia</span> ';
		#: '';

	for ($page = 1; $page <= $pageCount; ++$page)
	{
		$result .= $page === $currentPage
			? '<span class="page-current">' . $page . '</span>'
			: '<a class="page" href="' . $ro->gen($route, array_merge(array($param => $page), $params)) . '">' . $page . '</a>';

		# $result .= $page < $pageCount ? ' | ' : ' ';
	}

	$result .= $currentPage < $pageCount
		? '<a class="page-next" href="' . $ro->gen($route, array_merge(array($param => $currentPage + 1), $params)) . '">Następna &rsaquo;</a> '
		: '<span class="page-next">Następna &rsaquo;</span> ';
		#: '';

	$result .= $currentPage < ($pageCount - 1)
		? '<a class="page-last" href="' . $ro->gen($route, array_merge(array($param => $pageCount), $params)) . '">Ostatnia &raquo;</a>'
		: '<span class="page-last">Ostatnia &raquo;</span>';
		#: '';

	return $result;
}